package lib.weixininteract;

import com.alibaba.fastjson.JSONArray;
import lib.Dao.TokenDao;
import lib.servlet.AuthUtil;
import net.sf.json.JSONObject;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class CreatMenu {
    public String CreatMenu(String openid,String changetype,String coursedata) throws SQLException {

        TokenDao tokenDao = new TokenDao();
        // 获取token
        String token = null;
        try {
            token = tokenDao.gettoken();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        String postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token;
        JSONObject postdata = new JSONObject();

        JSONObject jsonObject = new JSONObject();
        JSONArray button = new JSONArray();
        jsonObject.put("type","view");   // openid
        jsonObject.put("name", "课程预约");
        jsonObject.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx291b25f289a25814&redirect_uri=http://www.eyehelper.top/CallBackServlet&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
        button.add(jsonObject);

        JSONObject data = new JSONObject();
        JSONObject first = new JSONObject();
        first.put("name", "快捷菜单");
        JSONArray sub_button = new JSONArray();
        JSONObject keyword1 = new JSONObject();
        keyword1.put("type", "view");
        keyword1.put("name", "开始选课");
        keyword1.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx291b25f289a25814&redirect_uri=http://www.eyehelper.top/CallBackList&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
        JSONObject keyword2 = new JSONObject();
        keyword2.put("type", "view");
        keyword2.put("name", "我的订单");
        keyword2.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx291b25f289a25814&redirect_uri=http://www.eyehelper.top/CallBackOrder&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
        sub_button.add(keyword1);
        sub_button.add(keyword2);
        first.put("sub_button",sub_button);
        button.add(first);

        JSONObject keyword3 = new JSONObject();
        keyword3.put("color", "#173177");
        JSONObject remark = new JSONObject();
        remark.put("value", coursedata);
        remark.put("color", "#173177");

        data.put("first", first);
        data.put("keyword1", keyword1);
        data.put("keyword2", keyword2);
        data.put("keyword3", keyword3);
        data.put("remark", remark);
        postdata.put("button",button);
        jsonObject.put("data", data);
        String json = jsonObject.toString();
        //System.out.println(json);
        String result = AuthUtil.HttpPostWithJson(postUrl, json);
        //System.out.println(result);
        return result;
    }
}

